package com.example.demoback.web;

import com.example.demoback.entity.*;
import com.example.demoback.mapper.TypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("type")
public class TypeAction {

    @Autowired
    TypeMapper typeMapper;


    @RequestMapping("queryAll")
    List<Map<String, Object>>queryAll(){
        return typeMapper.selectAll();
    }
    @RequestMapping()
    Category getType(String name){
        return typeMapper.selectByIdAndName(name);
    }

    @RequestMapping("getType")
    BigType getType(){
        List<Category> categories = typeMapper.selectAllCategories();
        List<SmallType> smallTypes = new java.util.ArrayList<>(List.of());
        for(Category category : categories){
            List<Subcategory> subs = typeMapper.selectSubCategoriesByCategoryId(category.getCategoryID());
            SmallType smallType = new SmallType(category.getCategoryID(),category.getCategoryName(),subs);
            smallTypes.add(smallType);
        }
        return new BigType(smallTypes);
    }

    @RequestMapping("getItem")
    Result getImg(int id){
        List<Map<String, Object>> items = typeMapper.selectItemsBySubcategoryId(id);
        return new Result(1,"查询物品信息成功",items);

    }

    @RequestMapping("getItemByIdAndName")
    Result getItemByName(int id,String name){
        List<Map<String, Object>> items = typeMapper.selectItemsBySubcategoryIdAndName(id,name);
        if(items == null){
            throw new RuntimeException("没找到商品");
        }
        return new Result(1,"查询物品信息成功",items);
    }
}
